/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web.client.servlet;

import com.imenu.ejb.ImenuCustomerFacade;
import com.imenu.entity.ImenuCustomer;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "ClientProfileServlet", urlPatterns = {"/clientProfile"})
public class ClientProfileServlet extends BaseServlet<ImenuCustomer> {

    @EJB
    ImenuCustomerFacade customerFacade;

    @Override
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("com.imenu.web.client.ClientMenuServlet.defaultAction() request.getRequestURL() = " + request.getRequestURL());
        //customerFacade.find(this);

        this.forward(request, response, "/profile/profile.jsp");
    }

    void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("com.imenu.web.client.ClientMenuServlet.defaultAction() request.getRequestURL() = " + request.getRequestURL());
        //customerFacade.find(this);

        this.forward(request, response, "/profile/register.jsp");
    }

    void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("com.imenu.web.client.ClientMenuServlet.defaultAction() request.getRequestURL() = " + request.getRequestURL());
        //customerFacade.find(this);

        this.forward(request, response, "/profile/login.jsp");
    }

    void androidProfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() request.getRequestURL() = " + request.getRequestURL());
        //customerFacade.find(this);
        String deviceId = request.getParameter("deviceId");
        ImenuCustomer customer = customerFacade.findByDeviceId(deviceId);
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() deviceId = " + deviceId);
        String command = "edit";
        if (customer == null) {
            command = "create";
        }
        request.setAttribute("customer", customer);
        this.forward(request, response, "/profile/androidProfile.jsp?action=" + command);
    }

    void androidSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() request.getRequestURL() = " + request.getRequestURL());
        //customerFacade.find(this);
        String deviceId = request.getParameter("deviceId");
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() deviceId = " + deviceId);
        String userid = request.getParameter("id");
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() userid = " + userid);
        String action = request.getParameter("action");
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() action = " + action);
        String nickname = request.getParameter("nickname");
        System.out.println("com.imenu.web.client.ClientProfileServlet.androidProfile() nickname = " + nickname);
        ImenuCustomer customer = customerFacade.findByDeviceId(deviceId);
        if (customer == null) {
            customer = new ImenuCustomer();
            customer.setDeviceId(deviceId);
            customer.setNickname(nickname);
            customerFacade.create(customer);
            request.setAttribute("message", "创建新用户成功。");
        } else {
            customer.setNickname(nickname);
            customerFacade.edit(customer);
            request.setAttribute("message", "更改用户名成功。");
        }
        request.setAttribute("customer", customer);
        this.forward(request, response, "/profile/androidProfile.jsp");
    }
    
    void profileRemove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deviceId = request.getParameter("deviceId");
        ImenuCustomer customer = customerFacade.findByDeviceId(deviceId);
        if(customer != null){
            customerFacade.remove(customer);
        }
    }
}
